package com.jwgf.smarowindingmachine_unity.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import com.jwgf.adapter.CommonAdapter;
import com.jwgf.adapter.ViewHolder;
import com.jwgf.fault.FaultRecorder;
import com.jwgf.objectbox.HeadFaultRecord;
import com.jwgf.objectbox.HeadFaultRecord_;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.ProcessRecord;
import com.jwgf.objectbox.ProcessRecord_;
import com.jwgf.objectbox.RedLightRecord;
import com.jwgf.objectbox.RedLightRecord_;
import com.jwgf.smarowindingmachine_unity.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import androidx.fragment.app.DialogFragment;

import io.objectbox.Box;

public class FaultDetailDiaInStatisticsReport extends DialogFragment {

	private ListView faultLV;
	private List<HashMap<String, String>> faultListData = new ArrayList<HashMap<String, String>>();
	private CommonAdapter adapter;
	private static final String START_TIME = "start_time";
	private static final String END_TIME = "end_time";
	private long startTime = 0;
	private long endTime = 0;

	@Override
	public void onResume() {
		super.onResume();
		getDialog().getWindow().setLayout(640, 680);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View v = inflater.inflate(R.layout.dialog_fault_detail_in_shift_report, null);
		faultLV = (ListView) v.findViewById(R.id.fault_detail_list);

		Bundle bd = getArguments();
		startTime = bd.getLong(START_TIME);
		endTime = bd.getLong(END_TIME);

		final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		faultLV.setAdapter(adapter = new CommonAdapter<HashMap<String, String>>(getActivity(), getFaultListData(),
				R.layout.item_fault_statistic_list) {

			@Override
			public void convert(ViewHolder holder, HashMap<String, String> item) {
				StringBuilder builder = new StringBuilder();
				if (item.containsKey("spindle")) {
					builder.append(getResources().getString(R.string.spindle)).append(item.get("spindle"));
				}
				String content = builder.append(FaultRecorder.getInstance().getFault(Integer.valueOf(item.get("fault_code")))).toString();
				holder.setText(R.id.fault_string, content);
				long startTime = Long.valueOf(item.get("start_time"));
				Calendar calStart = Calendar.getInstance();
				calStart.setTimeInMillis(startTime);
				holder.setText(R.id.fault_column1, sdf.format(calStart.getTime()));
				long endTime = Long.valueOf(item.get("end_time"));
				Calendar calEnd = Calendar.getInstance();
				calEnd.setTimeInMillis(endTime);
				holder.setText(R.id.fault_column2, sdf.format(calEnd.getTime()));
			}

		});
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("故障详情");
		builder.setView(v);
		return builder.create();
	}

	private List<HashMap<String, String>> getFaultListData() {

		faultListData.clear();

		Box<RedLightRecord> redLightBox = ObjectBox.get().boxFor(RedLightRecord.class);
		List<RedLightRecord> redList = redLightBox.query().between(RedLightRecord_.start, startTime, endTime)
				.orderDesc(RedLightRecord_.start)
				.build().find();
		Box<ProcessRecord> processBox = ObjectBox.get().boxFor(ProcessRecord.class);
		List<ProcessRecord> processList = processBox.query().between(ProcessRecord_.start, startTime, endTime)
				.orderDesc(ProcessRecord_.start)
				.build().find();
		Box<HeadFaultRecord> headFaultBox = ObjectBox.get().boxFor(HeadFaultRecord.class);
		List<HeadFaultRecord> headFaultList = headFaultBox.query().between(HeadFaultRecord_.start, startTime, endTime)
				.orderDesc(HeadFaultRecord_.start)
				.build().find();
		ArrayList<HashMap<String, String>> dataList = new ArrayList<>();
		for (RedLightRecord record : redList) {
			HashMap<String, String> item = new HashMap<String, String>();
			item.put("spindle", String.valueOf(record.getSpindleId()));
			item.put("fault_code", String.valueOf(record.getFaultCode()));
			item.put("start_time", String.valueOf(record.getStart()));
			item.put("end_time", String.valueOf(record.getEnd()));
			dataList.add(item);
		}
		for (ProcessRecord record : processList) {
			HashMap<String, String> item = new HashMap<String, String>();
			item.put("spindle", String.valueOf(record.getSpindleId()));
			item.put("fault_code", String.valueOf(record.getFaultCode()));
			item.put("start_time", String.valueOf(record.getStart()));
			item.put("end_time", String.valueOf(record.getEnd()));
			dataList.add(item);
		}
		for (HeadFaultRecord record : headFaultList) {
			HashMap<String, String> item = new HashMap<String, String>();
			item.put("fault_code", String.valueOf(record.getFaultCode()));
			item.put("start_time", String.valueOf(record.getStart()));
			item.put("end_time", String.valueOf(record.getEnd()));
			dataList.add(item);
		}

		faultListData.addAll(dataList);

		return faultListData;

	}

}
